package com.example.studyllm.ollama;

import cn.hutool.json.JSONUtil;
import com.example.studyllm.utils.HttpClientUtils;
import com.example.studyllm.utils.HttpQueryParams;
import com.example.studyllm.vo.base.Result;
import dev.langchain4j.community.model.dashscope.QwenChatModel;
import dev.langchain4j.community.model.dashscope.QwenStreamingChatModel;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.apache.tika.ApacheTikaDocumentParser;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.image.ImageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.model.ollama.OllamaStreamingChatModel;
import dev.langchain4j.service.AiServices;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author wk
 */
@RestController
@RequestMapping("/ollama")
public class ChatController {

    @GetMapping(value="/chatStream")
    public String chatStream(){
        Document document;
        document = FileSystemDocumentLoader.loadDocument("D:\\360安全浏览器下载\\q.pdf", new ApacheTikaDocumentParser());
        ChatLanguageModel model = buildModel();
        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
                .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
              //  .tools(new WetherTool())
                .build();
        return assistant.chat("这是一个后端开发需求文档，帮忙分析文档，并输出涉及到的mysql表结构，基于springboot的接口，接口包含入参出参:"+document.text());
    }

    @GetMapping("/testHrttp")
    public String testHrttp(){
        String url = "http://localhost:8888/ollama/hello";
        return JSONUtil.toJsonStr(HttpClientUtils.sendGet(HttpQueryParams.builder().url(url).build()));
    }
    @GetMapping("/hello")
    public Result sayhello(){
        return Result.success("hello");
    }

    private ChatLanguageModel buildTongyiModel(){
        return QwenChatModel.builder()
                .modelName("qwq-32b-preview")
                .apiKey("sk-0c9ec11299e04a8c95434b5d083a7d54")
                .build();
    }

    private StreamingChatLanguageModel buildTongyiStreamModel(){
        return QwenStreamingChatModel.builder()
                .modelName("qwq-32b-preview")
                .apiKey("sk-0c9ec11299e04a8c95434b5d083a7d54")
                .build();
    }

    private StreamingChatLanguageModel buildStreamModel(){
        return OllamaStreamingChatModel.builder()
                .baseUrl("http://60.190.170.54:11434")
                .modelName("deepseek-r1:14b")
                .temperature(0.1)
                .logRequests(true)
                .logResponses(true)
                .build();
    }



    private ChatLanguageModel buildModel(){
        return OllamaChatModel.builder()
                .baseUrl("http://60.190.170.54:11434")
                .modelName("deepseek-r1:14b")
                .temperature(0.1)
                .build();
    }
}
